﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.UI;
using DataConDemoWebRole.Models;
using System.Diagnostics;
using DataConDemoWebRole.Business;
using Microsoft.WindowsAzure.ServiceRuntime;
using System.Configuration;

namespace DataConDemoWebRole.Controllers
{
    /// <summary>
    /// This controller gets item data using RESTful addressing.
    /// All results are Json Results, so we use HandleAjaxError on the class, 
    /// which means it applies to all actions.
    /// </summary>
    [HandleAjaxError]
    [OutputCache(Location = OutputCacheLocation.None)]
    public class DataController : Controller
    {

        // return items within a bounding box
        [HttpPost]
        [CompressFilter]
        public ActionResult GetData(string[] tables, BoundingBox box)
        {
            var ds = new DataService();
            BoundingBoxQuery query = new BoundingBoxQuery()
            {
                Box = GeoJsonConvert.GeographyFromBoundingBox(box),
                Tables = tables
            };
            var data = ds.GetData(query);
            Debug.WriteLine("Viewport data returned");
            //use the custom largejsonresult so that we can set a maxjsonlength property
            return WriteLargeJson(data);
        }

        // return items near a point
        [HttpPost]
        [CompressFilter]
        public ActionResult GetDataProximity(string[] tables, BoundingBox box, Models.GeoJson.Point point, double distance)
        {
            var ds = new DataService();
            BoundingBoxQuery query = new BoundingBoxQuery()
            {
                Tables = tables,
                Box = GeoJsonConvert.GeographyFromBoundingBox(box),
                Shape = GeoJsonConvert.GeographyFromGeoJsonPoint(point),
                Distance = distance
            };
            var data = ds.GetData(query);
            Debug.WriteLine("Proximity data returned");
            //use the custom largejsonresult so that we can set a maxjsonlength property
            return WriteLargeJson(data);
        }

        // return items within an area
        [HttpPost]
        [CompressFilter]
        public ActionResult GetDataArea(string[] tables, BoundingBox box, Models.GeoJson.Polygon area)
        {
            var ds = new DataService();
            BoundingBoxQuery query = new BoundingBoxQuery()
            {
                Tables = tables,
                Box = GeoJsonConvert.GeographyFromBoundingBox(box),
                Shape = GeoJsonConvert.GeographyFromGeoJsonPolygon(area),
            };
            var data = ds.GetData(query);
            Debug.WriteLine("Area data returned");
            //use the custom largejsonresult so that we can set a maxjsonlength property
            return WriteLargeJson(data);
        }

        // return items near a line
        [HttpPost]
        [CompressFilter]
        public ActionResult GetDataLine(string[] tables, BoundingBox box, Models.GeoJson.LineString line, double distance)
        {
            var ds = new DataService();
            BoundingBoxQuery query = new BoundingBoxQuery()
            {
                Tables = tables,
                Box = GeoJsonConvert.GeographyFromBoundingBox(box),
                Shape = GeoJsonConvert.GeographyFromGeoJsonLineString(line),
                Distance = distance
            };
            var data = ds.GetData(query);
            Debug.WriteLine("Line data returned");
            //use the custom largejsonresult so that we can set a maxjsonlength property
            return WriteLargeJson(data);
        }

        /// <summary>
        /// Method used to write out MapDataModel JSON setting max length property from settings files
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        private LargeJsonResult WriteLargeJson(MapDataModel data)
        {
            int maxjsonlength = 0;
            if (RoleEnvironment.IsAvailable)//check if running as azure role
            {
                //get maxjsonlength setting from azure config
                int.TryParse(RoleEnvironment.GetConfigurationSettingValue("maxjsonlength"), out maxjsonlength);
                return new LargeJsonResult() { Data = data, MaxJsonLength = maxjsonlength };
            }
            else
            {
                //get maxjsonlength from web.config
                
                int.TryParse(ConfigurationManager.AppSettings["maxjsonlength"],out maxjsonlength);
                return new LargeJsonResult() { Data = data, MaxJsonLength = maxjsonlength };
            }
        }

    }
}
